AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

include('shared.lua')

util.PrecacheModel( "models/Combine_turrets/Floor_turret.mdl" )


function ENT:FakeInit()

    self:SetSkin( 3 )
    
    //self:SetModel("models/Combine_turrets/Floor_turret.mdl")
    
    self.NextSearch = CurTime()
    
    self.Enemy = nil
    
    self.NextShoot = 0
    self.BulletsLeft = self.BulletsPerRound
    self.NextBullet = 0

    self.AimDirection = Vector()
    self:LoadSettings()
end

function ENT:IsEnemy( BonePos, ent )
    
    return ent:Alive() && self:Visible( ent ) && self:InConeView( BonePos, ent )
    
end


function ENT:FireBullet()
    
    local bullet = {}
    bullet.Attacker = self:FWOwner()
    bullet.Num=1
    bullet.Src= self:GetBonePosition( self.EyeBonePos )  
    bullet.Dir= self.AimDirection
    bullet.Spread= Vector(0.01,0.01,0)
    bullet.Tracer=1
    bullet.Force=2
    bullet.Damage= self.BulletDamage
   
    self:FireBullets(bullet)
end


function ENT:Shooting()
    if self.NextShoot < CurTime() then
        
        self:UpdateSeq( "fire" )
        
        if self.BulletsLeft > 0 then
            self:FireBullet()
            
            self.BulletsLeft = self.BulletsLeft - 1     
            
            if self.BulletsLeft == 0 then
                self.BulletsLeft = 2
                self.NextShoot = CurTime() + self.CoolDownTime     
                self:UpdateSeq( "idlealert" )
                
                //Make a cool smoke effect
                //local ed = EffectData()
            	//	ed:SetEntity( self )
            	//util.Effect( "sentry_smoke", ed, true, true )
            end   
            
        end
    
    
    end
end



function ENT:Think()
    if OnBuild() then return end
    if self:IsBuilding() then return end

    if CurTime() > self.NextSearch  then
        self.NextSearch = CurTime() + 0.3
        
        local BonePos = self:GetBonePosition( self.EyeBonePos ) 
        
        
        if IsValid(self.Enemy) then
            
            //Check if the enemy is still alive
            if !self:IsEnemy( BonePos ,self.Enemy ) then
                self:SetTarget( nil )
            else
                //Think about shooting
                self:Shooting()            
            
                return
            end
        
        end
        
        
        //If nothing, look for new enemies
        //Find the closest one
        local PosibleEnemy = nil
        local DistanceEnemey = self.Range
        
        for _, v in pairs( player.GetAll() ) do    
            if self:IsEnemy( BonePos, v ) && self:GetTeam() != v:Team()  then
                
                local Dista = v:EyePos():Distance( BonePos )
                
                if Dista < DistanceEnemey then
                    PosibleEnemy = v
                    DistanceEnemey = Dista
                end
                
            end
        end
        
        self:SetTarget( PosibleEnemy )
        
        
    end

end

function ENT:UpdateSeq( seq )
    
    local sequence = self.Entity:LookupSequence( seq ) 
    
    if self.Entity:GetSequence() != sequence then
        self.Entity:SetSequence( sequence )  
    end
    
end


function ENT:SetTarget( ply )
    if ply == self.Enemy then return end
    
    if ply == nil then
        self:UpdateSeq( "idle" )
    else
        self:UpdateSeq( "deploy" )
    end   
    
    self.Enemy = ply
    self.NextShoot = CurTime() + self.CoolDownTime
    
    self:SetNWEntity("trg", ply )
    self:SetNWBool("Vtrg", IsValid(ply) )
    
end
